<?php

namespace app\console\controller;

use think\facade\Session;
use think\facade\Request;
use think\facade\Config;
use think\facade\Url;
use app\console\model\Admin;
use think\Controller;

class Login extends Controller
{

    public function index()
    {

        return $this->redirect('userlogin');
    }

    /**
     * 登录
     */
    public function userlogin()
    {
        Session::has('consoler', 'console') == true && $this->redirect('console/index/index');
        return $this->fetch();
    }

    /**
     * 验证登录
     */
    public function check_login()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();

            // 调试模式 不验证验证码
           // echo base64_encode('123456');die;
            if (Config::get('app_debug') == false) {
                if (!captcha_check($data['verify'])) {
                    $this->error('验证码错误');
                };
            }

            if ($data['username'] == base64_decode(Config::get('authority.super_user')) && $data['password'] == base64_decode(Config::get('authority.command'))) {
                $user['id'] = 0;
                $user['username'] = $data['username'];
                Session::set('consoler', $user, 'console');
                $this->success('登陆成功', Url::build('console/index/index'));
            } else {
                $check_login_res = Admin::check_login($data['username'], $data['password']);
                if ($check_login_res['code'] != 1) $this->error($check_login_res['msg']);
                $this->success('登陆成功', Url::build('console/index/index'));
            }
            exit;
        }
        $this->redirect('userlogin');
    }

}
